window.addEventListener('load', function () {
    // 获取元素
    var banner_carousel = document.querySelector('.banner-carousel')
    var img_list = banner_carousel.querySelector('.img-list');
    var pointer = banner_carousel.querySelector('.pointer');
    var arrow_l = banner_carousel.querySelector('.arrow_l');
    var arrow_r = banner_carousel.querySelector('.arrow_r');

    var bannerWidth = banner_carousel.offsetWidth;
    // 动态创建小圆圈
    for (var i = 0; i < img_list.children.length; i++) {
        var li = document.createElement('li');
        li.setAttribute('index', i);
        pointer.appendChild(li);

        // 绑定点击事件
        li.addEventListener('click', function () {
            for (var i = 0; i < pointer.children.length; i++) {
                pointer.children[i].className = '';
            }
            this.className = 'current';
            var index = this.getAttribute('index');
            num = index;
            circle = index;
            animate(img_list, -index * bannerWidth);
        });
    }

    // 克隆第一张照片
    var first = img_list.children[0].cloneNode(true);
    img_list.appendChild(first);
    var num = 0;
    var circle = 0;
    var flag = true; // 节流阀 播放完上一张才播放下一张
    // 右侧按钮
    arrow_r.addEventListener('click', function () {
        if (flag) {
            flag = false;
            if (num == img_list.children.length - 1) {
                num = 0;
                img_list.style.left = 0;
            }
            num++;
            animate(img_list, -num * bannerWidth, function () {
                flag = true;
            });
            circle++;
            if (circle == pointer.children.length) {
                circle = 0;
            }
            for (var i = 0; i < pointer.children.length; i++) {
                pointer.children[i].className = '';
            }
            pointer.children[circle].className = 'current';
        }
    });

    // 左侧按钮
    arrow_l.addEventListener('click', function () {
        if (flag) {
            if (num == 0) {
                flag = false;
                num = img_list.children.length - 1;
                img_list.style.left = -num * bannerWidth + 'px';
            }
            num--;
            animate(img_list, -num * bannerWidth, function () {
                flag = true;
            });

            circle--;
            if (circle < 0) {
                circle = pointer.children.length - 1;
            }
            for (var i = 0; i < pointer.children.length; i++) {
                pointer.children[i].className = '';
            }
            pointer.children[circle].className = 'current';
        }

    });
    pointer.children[0].className = 'current';

    // 自动播放
    var timer = setInterval(function () {
        // 手动调用点击事件
        arrow_r.click();
    }, 3000);

    // 鼠标移入
    banner_carousel.addEventListener('mouseenter', function () {
        clearInterval(timer);
        timer = null;
    });

    // 鼠标移出
    banner_carousel.addEventListener('mouseleave', function () {
        timer = setInterval(function () {
            // 手动调用点击事件
            arrow_r.click();
        }, 3000);
    });
});

